package com.gopay.common.log.logxnew.appender;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.Appender;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.spi.LoggingEvent;

import javax.jms.*;

/**
 * log4j自定义activeMq queue模式Appender
 *
 * @OperLogJMSAppender.java
 * @author sh.jiao
 * @2017-5-24 上午11:09:49 www.gopay.com.cn Inc.All rights reserved.
 */
public class OperLogJMSAppender extends AppenderSkeleton implements Appender {

    private String brokerUri;
    private String userName;
    private String password;
    private String queueName;

    private Session session;
    private Connection connection;
    private MessageProducer producer;


    @Override
    protected void append(LoggingEvent event) {
        try {

            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(this.userName, this.password,
                    this.brokerUri);

            // Create a Connection
            this.connection = connectionFactory.createConnection();
            connection.start();

            // Create a Session
            this.session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            // Create the destination (Topic or Queue)
            Destination destination = session.createQueue(this.queueName);

            // Create a MessageProducer from the Session to the Topic or Queue
            this.producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.PERSISTENT);

            ObjectMessage message = session.createObjectMessage();
            message.setObject(event);
            // Tell the producer to send the message
            producer.send(message);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void close() {
        if(this.closed){
            return;
        }
        LogLog.debug("Closing appender ["+name+"].");
        this.closed = true;
        try {
            if(session != null) {
                session.close();
            }
            if(connection != null) {
                connection.close();
            }
        } catch(JMSException e) {
            LogLog.error("Error while closing JMSAppender ["+name+"].", e);
        } catch(RuntimeException e) {
            LogLog.error("Error while closing JMSAppender ["+name+"].", e);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // Help garbage collection
        producer = null;
        session = null;
        connection = null;
    }

    @Override
    public boolean requiresLayout() {
        return false;
    }

    public String getBrokerUri() {
        return brokerUri;
    }

    public void setBrokerUri(String brokerUri) {
        this.brokerUri = brokerUri;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getQueueName() {
        return queueName;
    }

    public void setQueueName(String queueName) {
        this.queueName = queueName;
    }

    public Session getSession() {
        return session;
    }

    public void setSession(Session session) {
        this.session = session;
    }

    public Connection getConnection() {
        return connection;
    }

    public void setConnection(Connection connection) {
        this.connection = connection;
    }

    public MessageProducer getProducer() {
        return producer;
    }

    public void setProducer(MessageProducer producer) {
        this.producer = producer;
    }
}
